﻿using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using Habi.Solver.Common;
using Habi.Solver.Models.Resources;
using Habi.Solver.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DotNetExtensions.AspNet.DevExpress.Services;
using DotNetExtensions.AspNet.DevExpress.MVVM.ViewModels;

namespace Habi.Solver.ViewModels.Resources
{
    public class ResourceTaskViewModel : SingleObjectViewModel<ResourceTaskModel, string>
    {
        public IMessageService Notify { get { return this.GetService<IMessageService>(); } }

        public virtual IList<ResourceTaskModel> Tasks { get; set; }

        public virtual IList<ResourceTaskModel> Selected { get; set; }

        public ResourceTaskViewModel()
        {
            Messenger.Default.Register<ResourceTaskModel>(this, task =>
            {
                Console.WriteLine("ResourceTaskViewModel, {0}", task);
            });

            TaskDownloader.Default.OnTaskCompleted += e =>
            {
                Console.WriteLine("Task {0} download/upload completed", e.Task.Id);

                if (e.OK && File.Exists(e.DestFileName))
                {
                    var dialog = Notify.Confirm("Local file was exists , are you wan't replaced?");
                    if (dialog == System.Windows.Forms.DialogResult.OK)
                    {
                        File.Delete(e.DestFileName);
                        File.Move(e.FileName, e.DestFileName);
                    }
                }
                else if (e.OK)
                {
                    File.Move(e.FileName, e.DestFileName);
                }

            };
            TaskDownloader.Default.OnTaskProgressChanged += e =>
            {
                Console.WriteLine("Task {0}, Progress {1:P0}", e.Task.Id, e.Task.Progress);
            };

            TaskContainer.Default.OnTaskChanged += (e) =>
            {
                Tasks = TaskContainer.Default.Tasks.Values.ToList();
            };
        }

        public void Load()
        {
            Tasks = TaskContainer.Default.Tasks.Values.ToList();
        }

        public void Start()
        {
            if (Selected == null || Selected.Count == 0) return;
            ResourceTaskModel[] temp = new ResourceTaskModel[Selected.Count];
            Selected.CopyTo(temp, 0);

            foreach (var item in temp)
            {
                TaskContainer.Default.Start(item);
            }
        }

        public void Pause()
        {
            if (Selected == null || Selected.Count == 0) return;

            ResourceTaskModel[] temp = new ResourceTaskModel[Selected.Count];
            Selected.CopyTo(temp, 0);

            foreach (var item in temp)
            {
                TaskContainer.Default.Pause(item);
            }
        }

        public void Remove()
        {
            if (Selected == null || Selected.Count == 0) return;
            ResourceTaskModel[] temp = new ResourceTaskModel[Selected.Count];
            Selected.CopyTo(temp, 0);

            foreach (var item in temp)
            {
                TaskContainer.Default.Delete(item);
            }

            Selected.Clear();
        }
    }
}
